import { computed } from 'vue'
import { useAuthStore } from '@/stores/auth'
import { usePermissionStore } from '@/stores/permission'

export function usePermission() {
  const authStore = useAuthStore()
  const permissionStore = usePermissionStore()

  // 检查是否有指定权限
  const hasPermission = (permissionCode: string): boolean => {
    if (!authStore.isAuthenticated) {
      return false
    }

    // 超级用户拥有所有权限
    if (authStore.user?.is_superuser) {
      return true
    }

    return permissionStore.hasPermission(permissionCode)
  }

  // 检查是否有任意一个权限
  const hasAnyPermission = (permissionCodes: string[]): boolean => {
    if (!authStore.isAuthenticated) {
      return false
    }

    if (authStore.user?.is_superuser) {
      return true
    }

    return permissionStore.hasAnyPermission(permissionCodes)
  }

  // 检查是否有所有权限
  const hasAllPermissions = (permissionCodes: string[]): boolean => {
    if (!authStore.isAuthenticated) {
      return false
    }

    if (authStore.user?.is_superuser) {
      return true
    }

    return permissionStore.hasAllPermissions(permissionCodes)
  }

  // 检查是否有指定角色
  const hasRole = (roleCode: string): boolean => {
    if (!authStore.isAuthenticated) {
      return false
    }

    if (authStore.user?.is_superuser) {
      return true
    }

    return permissionStore.hasRole(roleCode)
  }

  // 检查是否有任意一个角色
  const hasAnyRole = (roleCodes: string[]): boolean => {
    if (!authStore.isAuthenticated) {
      return false
    }

    if (authStore.user?.is_superuser) {
      return true
    }

    return permissionStore.hasAnyRole(roleCodes)
  }

  // 检查是否是管理员
  const isAdmin = computed(() => {
    return authStore.user?.is_superuser || hasRole('admin')
  })

  // 检查是否是普通用户
  const isUser = computed(() => {
    return hasRole('user')
  })

  // 权限指令（用于v-if等）
  const canAccess = (permission: string | string[], requireAll = false): boolean => {
    if (Array.isArray(permission)) {
      return requireAll ? hasAllPermissions(permission) : hasAnyPermission(permission)
    }
    return hasPermission(permission)
  }

  return {
    hasPermission,
    hasAnyPermission,
    hasAllPermissions,
    hasRole,
    hasAnyRole,
    isAdmin,
    isUser,
    canAccess
  }
}

// 权限常量
export const PERMISSIONS = {
  // 用户管理
  USER_VIEW: 'user_view',
  USER_CREATE: 'user_create',
  USER_UPDATE: 'user_update',
  USER_DELETE: 'user_delete',
  USER_MANAGE: 'user_manage',

  // 角色管理
  ROLE_VIEW: 'role_view',
  ROLE_CREATE: 'role_create',
  ROLE_UPDATE: 'role_update',
  ROLE_DELETE: 'role_delete',
  ROLE_MANAGE: 'role_manage',

  // 权限管理
  PERMISSION_VIEW: 'permission_view',
  PERMISSION_CREATE: 'permission_create',
  PERMISSION_UPDATE: 'permission_update',
  PERMISSION_DELETE: 'permission_delete',
  PERMISSION_MANAGE: 'permission_manage',

  // 菜单管理
  MENU_VIEW: 'menu_view',
  MENU_CREATE: 'menu_create',
  MENU_UPDATE: 'menu_update',
  MENU_DELETE: 'menu_delete',
  MENU_MANAGE: 'menu_manage',

  // 系统监控
  SYSTEM_MONITOR: 'system_monitor',
  LOG_VIEW: 'log_view',

  // CMDB管理
  CMDB_VIEW: 'cmdb_view',
  CMDB_MANAGE: 'cmdb_manage',
  ASSET_VIEW: 'asset_view',
  ASSET_CREATE: 'asset_create',
  ASSET_UPDATE: 'asset_update',
  ASSET_DELETE: 'asset_delete',
} as const

// 角色常量
export const ROLES = {
  ADMIN: 'admin',
  USER: 'user',
  OPERATOR: 'operator',
  VIEWER: 'viewer',
} as const
